package br.composer.util;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;



public class ImageCreator {

	public static void main(String[] args){
		ArrayList<String> l = new ArrayList<String>(); 
		l.add("pop-ba");l.add("pop-mg");l.add("pop-df"); l.add("pop-rj");
		l.add("pop-sp"); //l.add("pop-pr");  //apenas para testes
		//l.add("pop-sc");l.add("pop-rs");
		double results[] = new double[l.size()];
		results[0]=10.222258; results[1]=1200.222258; results[2]=5.5822514; results[3]=8.88972214;
		
		ImageCreator img = new ImageCreator();
		try {
			img.imageGenerator(800	, 300, l,results, "pop-mg","%");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param width
	 * @param height
	 * @param pops todos os pops, inclusive src e dst
	 * @param results
	 * @param gargalo pop em que se inicia o gargalo (e.g: gargalo=pop-ba->pop-mg, passar pop-ba) 
	 * @param unidade (ms, kbps, mbps, %...etc)
	 * @return ImageIcon imagem
	 * @throws IOException caso nao seja possivel encontrar "images/computer.png"
	 */
	public ImageIcon imageGenerator(int width, int height, ArrayList<String> pops, double results[],
									String gargalo, String unidade) throws IOException{
		
		//FIXME definir width e height dinamicamente, sem a necessidade de ser recebido por parametro 
		int x1=40, y=120, esp=(width-70)/(pops.size()-1), lin=180;
		
        BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = buffer.createGraphics();
        Graphics2D g2d = (Graphics2D) g;
        
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        
        Font font = new Font("Courrier New", Font.BOLD, 15);
        g.setFont(font);
        BufferedImage bi = ImageIO.read(new File("images/computer.png"));
        
        for(int i=0; i<pops.size(); i++){
        	g.drawImage(bi,x1-20,58,null);
        	
        	g.setColor(Color.BLUE);
        	g.drawString(pops.get(i),x1-30,y-20);
        	
        	g.drawString(String.valueOf(i+1)+" - ",10,lin);
        	g.setColor(Color.BLACK);
        	g.drawString(pops.get(i),40,lin);
        	lin=lin+20;

        	if(pops.get(i).equalsIgnoreCase(gargalo)){
        		g2d.setPaint(Color.RED);
        	}
        	else{
        		g2d.setPaint(Color.GREEN);
        	}
	        
        	if (i<pops.size()-1){
	        	g2d.setStroke(new BasicStroke (4.0f));
		        g2d.draw(new Line2D.Double (x1, y, x1+esp, y));

		        //imprime o resultado (result)
		        g.setColor(Color.BLACK);
		        String text = new DecimalFormat("0.00").format(results[i])+" "+unidade;
		        int desl = (((x1+esp)-x1)/3)-(text.length()/2);
		        g.drawString(text, x1+desl, y+20); 
        	}
        	g.setColor(Color.BLACK);
        	g2d.drawRect(x1, y-3, 1, 9);
        	x1 = x1+esp;
        }
        
    	
    	g2d.setPaint(Color.RED);
    	g2d.draw(new Line2D.Double (width-220, lin-40, width-120, lin-40));
    	g.setColor(Color.BLACK);
    	g.drawString(" - Bottleneck",width-110,lin-40);
    	
        try {
			ImageIO.write(buffer, "png", new File("imgPath.png"));
			System.out.println("Imagem do caminho criada com sucesso...");
		} catch (IOException e) {
			System.err.println("!N�o foi poss�vel criar imagem do caminho...");
		}
		
        return new ImageIcon(buffer);
    }

}
